/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import crm.salesOrderProcessing.entity.LineItemEntity;
import crm.salesOrderProcessing.entity.SalesInquiryEntity;
import crm.salesOrderProcessing.session.SalesInquirySessionBean;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.event.ActionEvent;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

/**
 *
 * @author frankxgz
 */
@ManagedBean
@ViewScoped
public class CreateInquiryResultManagedBean implements Serializable {

    private SalesInquiryEntity inquiry;
    private List<LineItemEntity> lineItems;
    private Long inquiryId;
    private String message;
    @EJB
    SalesInquirySessionBean inquirySessionBean;

    /** Creates a new instance of CreateInquiryResultManagedBean */
    public CreateInquiryResultManagedBean() {
        inquirySessionBean = new SalesInquirySessionBean();
    }

    public void initView(PhaseEvent event) {

        if (inquiry == null) {

            inquiry = (SalesInquiryEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("newInquiry");
        }

        setLineItems(inquiry.getInquiryLineItems());
        setInquiryId(inquiry.getId());
//        for (int i=0;i<lineItems.size();i++){
//            double subtotal = lineItems.get(i).getPrice()*lineItems.get(i).getQuantity();
//            lineItems.get(i).setCumAmount(subtotal);
//        }
        message = "Sales Inquiry has been saved. ID: " + String.valueOf(inquiryId);

    }

    public void editInquiry() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("inquiry", inquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editSalesInquiry.xhtml");
    }

    public void goBack2() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteSalesInquiry.xhtml");
    }
    
    public void goBack3() throws IOException {
        FacesContext.getCurrentInstance().getExternalContext().redirect("./inquiryToQuotation.xhtml");
    }
    
    public void sendEmail(ActionEvent event){
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("jasperreports");
        exportToPDFBySQLConn(path);
        inquirySessionBean.sendSalesInquiry(inquiry.getCompany().getEmail(), inquiry, path);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Email has been sent.", ""));  
    }

    public SalesInquiryEntity getInquiry() {
        return inquiry;
    }

    public void setInquiry(SalesInquiryEntity inquiry) {
        this.inquiry = inquiry;
    }

    public List<LineItemEntity> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItemEntity> lineItems) {
        this.lineItems = lineItems;
    }

    public Long getInquiryId() {
        return inquiryId;
    }

    public void setInquiryId(Long inquiryId) {
        this.inquiryId = inquiryId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public void exportToPDFBySQLConn(String path){
        try {

            List<LineItemEntity> lineItemList;
            lineItemList = inquiry.getInquiryLineItems();
            
            JRBeanCollectionDataSource jrResultSetDataSource = new JRBeanCollectionDataSource(lineItemList);
            
            HashMap parameters = new HashMap();
            parameters.put("IMAGEPATH", "http://localhost:8080/MerlionFoodERP-war/jasperreports/wood.jpg");
            
            String fileName = path+"/lineItem.jasper";
            String outFileName = path+"/SalesInquiry_"+inquiry.getId()+".pdf";
            // Fill the report using an empty data source
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, jrResultSetDataSource);
            
            // Create a PDF exporter
            JRExporter exporter = new JRPdfExporter();
            
            // Configure the exporter (set output file name and print object)
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            
            // Export the PDF file
            exporter.exportReport();
        } 
        catch (JRException jrex){
            System.out.println("********** Jasperreport Exception");
            jrex.printStackTrace();
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
